package com.aaa.javaweb.dao.impl;

import com.aaa.javaweb.dao.TsTpDao;
import com.aaa.javaweb.entity.Tstp;
import com.aaa.javaweb.until.JDBCUntil;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;
@Repository
public class TsTpDaoImpl implements TsTpDao {
    @Override
    public List<Map> queryAll(Integer page, Integer limit, Tstp tstp) {

        String sql="select ts.id,ts.name,ts.count,ts.pmoney,ts.gys,tp.pname from t_sheshi ts, t_ptype tp where ts.typeid=tp.id ";
            if(tstp.getId()!=null){
                sql+=" and ts.id="+tstp.getId();
            }
            if(tstp.getName()!=null){
                sql+=" and  ts.name like '%"+tstp.getName()+"%'";
            }
            sql+=" limit ?,?";
        return JDBCUntil.selectMap(sql,(page-1)*limit,limit);
    }
    @Override
    public int queryCount(Tstp tstp) {
        String sql="select count(*) a from t_sheshi where 1=1 ";
        if(tstp.getId()!=null){
            sql+=" and id="+tstp.getId();
        }
        if(tstp.getName()!=null){
            sql+=" and name like '%"+tstp.getName()+"%'";
        }
        List<Map> maps = JDBCUntil.selectMap(sql);
        Object a = maps.get(0).get("a");
        int i = Integer.parseInt(a + "");
        return i;
    }

    @Override
    public int del(int id) {
        String sql="delete from t_sheshi where id=?";
        return JDBCUntil.Updateout(sql,id);
    }

    @Override
    public int add(Tstp tstp) {
        if(tstp.getId()==null){
            String sql="insert into t_sheshi(name,count,typeid,pmoney,gys)  values(?,?,?,?,?)";
            return JDBCUntil.Updateout(sql,tstp.getName(),tstp.getCount(),tstp.getTypeid(),tstp.getPmoney(),tstp.getGys());
        }else{
            String sql="update t_sheshi set name=?,count=?,typeid=?,pmoney=?,gys=? where id=?";
            return JDBCUntil.Updateout(sql,tstp.getName(),tstp.getCount(),tstp.getTypeid(),tstp.getPmoney(),tstp.getGys(),tstp.getId());
        }

    }
   @Override
    public List<Map> quer() {
        String sql="select * from t_ptype";
        return JDBCUntil.selectMap(sql);
    }



}
